package jse.log;

/**
 * 获取 Log 的静态工厂方法
 * @author Young(sunonfire@gmail.com)
 * @author zozoh(zozohtnt@gmail.com)
 * @author Wendal(wendal1985@gmail.com)
 */
public final class Logs {

    private static LogAdapter adapter;
    private static String className="jse.log.JseLog";

    static {
        init();
    }

    /**
     * Get a Log by Class
     * 
     * @param clazz
     *            your class
     * @return Log
     */
    public static Log getLog(Class<?> clazz) {
        return getLog(clazz.getName());
    }

    /**
     * Get a Log by name
     * 
     * @param className
     *            the name of Log
     * @return Log
     */
    public static Log getLog(String className) {
    	Logs.className=className;
        return adapter.getLogger(className);
    }

    /**
     * 返回以调用者的类命名的Log,是获取Log对象最简单的方法!
     */
    public static Log get() {
    	StackTraceElement[] sts = Thread.currentThread().getStackTrace();
    	return adapter.getLogger(sts[2].getClassName());
    }
    /**
     * 返回以调用者的类命名的Log,是获取Log对象最简单的方法!
     */
    public static Log get(Class<?> clazz) {
    	return getLog(clazz.getName());
    }

    /**
     * 初始化NutLog,检查全部Log的可用性,选择可用的Log适配器
     * <p/>
     * <b>加载本类时,该方法已经在静态构造函数中调用,用户无需主动调用.</b>
     * <p/>
     * <b>除非迫不得已,请不要调用本方法<b/>
     * <p/>
     */
    public static void init() {
            try {
                    adapter = (LogAdapter)Class.forName(className).getConstructor().newInstance();
            } catch (Throwable _e) {
                adapter = new JseLogAdapter();
            }
    }
    
    /**
     * 开放自定义设置LogAdapter,注意,不能设置为null!!
     * @param adapter 你所偏好的LogAdapter
     */
    public static void setAdapter(LogAdapter adapter) {
		Logs.adapter = adapter;
	}

	public static Log getLogger(Class<?> clazz) {
		return get(clazz);
	}
	public static Log getLogger(String clazz) {
		return getLog(clazz);
	}
}
